/**
 * Created by congzhang on 17/11/7.
 */

var express = require("express");
var path = require("path");
var fs = require("fs");
var bodyParser = require("body-parser");

var app = express();



app.set("view engine", 'ejs');  // 设置模板引擎
app.set("views", path.join(__dirname, "template"));    // 指定模板目录

app.use(express.static(path.join(__dirname, "statics"))); // 设置静态文件目录


// 将 post 请求的请求体转为对象
app.use(bodyParser.urlencoded({extended: true}));


app.get("/", function (req, res) {

    fs.createReadStream("./template/index.ejs").pipe(res)

});


app.get("/login", function (req, res) {

    fs.createReadStream("./template/login.html").pipe(res)

});


app.post("/login", function (req, res) {

    var username = req.body.username;
    var password = req.body.password;

    if(username == "zhangsan" && password == "123"){
        res.redirect("/home?username=" + username)
    }else{
        res.redirect("/login")
    }

});



app.get("/home", function (req, res) {

    res.render("home", {
        "username": req.query.username
    });

    fs.createReadStream("./template/home.ejs").pipe(res)

});
app.listen(8080, "0.0.0.0");